//
//  CYBaseTableViewController.swift
//  CYWeibo
//
//  Created by 媛 on 16/5/29.
//  Copyright © 2016年 yuan. All rights reserved.
//

//标签控制器的各子控制器 都继承自这个类，都可在加载页面前判断是否显示访客视图

import UIKit

//多继承
//OC中有多继承吗? 如果没有,用什么替代
//MARK: I.遵守代理协议
class CYBaseTableViewController: UITableViewController, CYVisitorLoginViewDelegate {

    //根据用户是否登录，判断显示登录页面还是访客视图页面
    var userLogin = false
    //添加访客视图的属性
    lazy var visitorLoginView: CYVisitorLoginView = CYVisitorLoginView()
    
    //MARK: 重写loadView，在这里自定义根视图
    //1. loadView方法会自动检测：跟视图是否为nil，如果为nil就调用loadView方法
    //2. 在view没有实例化之前，不要调用view的get方法
    //3. loadView方法，是苹果专门为手写代码准备的，一旦实现了这个方法，并且没有super，xib/sb就会自动失效        
    override func loadView() {
            if userLogin {
                //如果登陆，显示tableView
                //如果不super，就不会创建tableView
                super.loadView()
            } else {
                //显示访客视图
                self.loadVisitorView()
            }
    }
    
    private func loadVisitorView() {
       
        view = visitorLoginView
        //MARK: II.设置代理
        visitorLoginView.visitorDelegate = self
    }
    
    //MARK: III.实现协议方法
    func userWillLogin() {
        print("我要登陆")
        
        let oauthVC = CYOAuthViewController()
        //内嵌到导航视图控制器中
        let nav = UINavigationController(rootViewController: oauthVC)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    func userWillRegister() {
        print("我要注册")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

   
    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
